home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 44 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  51 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strange problem with strcpy()
  5. Date: 1 Jan 1996 01:39:27 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4c7viv$5vk@umbc9.umbc.edu>
  8. References: <DKFsII.FyJ@iglou.com> <4c7i49$q7b@news.infi.net>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Greg DiGiorgio <nngis@norfolk.infi.net> wrote:
  13. |> Now, what could cause your problem? As you know STRCPY is a dumb function 
  14. |> that copies bytes from whatever source address you give, up utnil it hits a 
  15. |> NULL to whatever destination address you give. Since you say you are using 
  16. |> MALLOC and/or REALLOC, my guess is that you are screwing up your pointer 
  17. |> arithmetic. It happens to everyone - experienced and newbie alike!
  18. |> 
  19. |> Let's assume that you have 2 vars, s1 and s2. s1 = "ABCD" and s2 = 
  20. |> (char)NULL. So if you "strcpy(s1,s2)" you'll get "\0BCD" in s1. If your 
  21. |> malloc fails, it returns a NULL which is easy enough to check. 
  22.  
  23. You seriously need to learn the difference between NULL and NUL. They
  24. are not the same and you should read the FAQ to see what I mean. The
  25. following code gives a segmentation fault on the UNIX system I am using
  26. which would tend to place your above assumption in doubt:
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. int main (void)
  32. {
  33.   char *s1 = "ABCD",
  34.        *s2 = NULL;
  35.  
  36.   printf ("%s\n", s1);
  37.   strcpy (s1, s2);
  38.   printf ("%s\n", s1);
  39.  
  40.   return (0);
  41. }
  42.  
  43. This demonstrates the critical difference between NULL and NUL. Had I
  44. wanted to achieve the desired result you claimed would occur I would
  45. need to change s2 to be = to "" instead of NULL. Like I said please read
  46. the FAQ before giving anymore incorrect advice.
  47. -- 
  48. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  49.  
  50. Jonas J. Schlein  (schlein@gl.umbc.edu)
  51.